import { useSessionStorage, useStorage } from '@vueuse/core'
import { defaultThemeConfig } from '@/utils/theme-config'
import { defaultAudioHistoryStorage } from '@/storage/defaults'
import type { RemovableRef } from '@vueuse/core'

/**
 * 根据环境设置 storage key
 * @param key
 */
export function getStorageKey(key: string): string {
  if (key === 'token') return key
  return `${import.meta.env.VITE_ENV}:${key}`
}

// 主题配置
export const themeConfig: RemovableRef<ThemeConfigType> = useStorage(
  getStorageKey('themeConfig'),
  defaultThemeConfig()
)

// AudioHistory
export const audioHistoryStorage: RemovableRef<AudioType[]> = useStorage(
  getStorageKey('audioHistoryStorage'),
  defaultAudioHistoryStorage()
)

// 用户token
export const token: RemovableRef<string | null> = useSessionStorage(getStorageKey('token'), null)
